<template>
  <a-spin :spinning="true" tip="加载中...">

  </a-spin>
</template>

<script>
import {ssoLogin, loginUserInfo} from '@/api/sso'
import storage from '@/utils/storage'

export default {
  name: 'sso',
  data () {
    return {
      token: '',
      auth: '',
      redirect: '',
    }
  },
  created () {
    this.token = this.$route.query.token
    this.redirect = this.$route.query.redirect
    storage.session.remove('gldToken')

    if (this.token) {
      storage.session.set('gldToken', this.token)
      ssoLogin({userToken: this.token, redirectURL: location.origin + '/sjsc' + '/#/sso'}).then(res => {
        if (res.code === 0) {
          this.auth = res.data
          if (this.auth) {
            // 用户信息接口 保存用户信息 跳转到首页
            storage.session.set('authToken', this.auth)
            //   "deptCode": "",
            //   "deptId": "",
            //   "deptName": "",
            //   "roles": [],
            //   "tenantId": "",
            //   "userCode": "",
            //   "userName": ""
            //   "phone": "",
            //   "email": ""
            loginUserInfo().then(res => {
              if (res.code === 0) {
                storage.session.set('userInfo', res.data)
                storage.session.set('isLogin', true)
                storage.session.set('roles', res.data.roles || [])
                if (res.data.roles && res.data.roles.length > 0) {
                  storage.session.set('role', res.data.roles[0])
      this.$store.commit('setRole', res.data.roles[0])

                }
                if (this.redirect) {
                  const query = JSON.parse(JSON.stringify(this.$route.query))
                  this.$delete(query, 'token')
                  this.$delete(query, 'redirect')
                  this.$router.push({
                    path: '/' + this.redirect,
                    query: {...query}
                  })
                } else {
                  this.$router.push('/portal')
                }
              }
            })
          }
        } else {
          location.href = res.data
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
